package cn.bonoon.kernel.web.html.grid;

import java.lang.reflect.AnnotatedElement;

import cn.bonoon.handler.AutoManager;
import cn.bonoon.kernel.IdGenerator;
import cn.bonoon.kernel.descriptors.ObjectDescriptor;
import cn.bonoon.kernel.support.entities.Persistable;
import cn.bonoon.kernel.support.models.AbstractAjaxNode;
import cn.bonoon.kernel.support.services.SearchService;
import cn.bonoon.kernel.web.annotations.grid.AsTreeGrid;
import cn.bonoon.kernel.web.handlers.ExecutableHandler;
import cn.bonoon.kernel.web.handlers.loader.AjaxNodeLoaderHandler;
import cn.bonoon.kernel.web.handlers.loader.NodeLoaderHandler;
import cn.bonoon.kernel.web.html.InternalHtmlInfo;

public class TreegridBuilder extends StandardGridBuilder{

	private boolean toggle;
	private String treeField;
	
	public TreegridBuilder(AsTreeGrid treegrid, ObjectDescriptor descriptor, AutoManager manager, InternalHtmlInfo html) {
		super("buptreegrid", treegrid.value(), descriptor, manager, html);
		__init(treegrid);
	}

	public TreegridBuilder(AsTreeGrid treegrid, ObjectDescriptor descriptor, AutoManager manager) {
		super("buptreegrid", treegrid.value(), descriptor, manager);
		__init(treegrid);
	}
	
	private void __init(AsTreeGrid treegrid){
		toggle = treegrid.toggle();
		idField = treegrid.idField();
		treeField = treegrid.treeField();
	}
	
	@Override
	protected <E extends Persistable> ExecutableHandler __loader(SearchService<E> searchService, AnnotatedElement aEle) throws Exception {
		if(uiDescriptor.isAssignable(AbstractAjaxNode.class)){
			return new AjaxNodeLoaderHandler(searchService, uiDescriptor.toClass(), aEle);	
		}else{
			return new NodeLoaderHandler(searchService, uiDescriptor.toClass(), aEle);
		}
	}
	
	@Override
	protected void initGridAttribute(InternalHtmlInfo html, StringBuilder gridAttribute) {
		//也是应该传些初始化值过去给控制器的，如：弹出对话框等，就不应该加载整个树列表
		super.initGridAttribute(html, gridAttribute);
		//如果是treegrid则使用本地排序
		html.append(" treeField='").append(treeField).append("'");
		gridAttribute.append(", showPagebar:false");
	}
	
	@Override
	protected String parseToolbar(InternalHtmlInfo html) {
		String tid = IdGenerator.fixed();
		html.block("jQuery('#").append(tid).append(" a').bupabutton({flat:true});");
		html.append("<div id='").append(tid).append("'>");
		if(toggle){
			html.append("<a href='#' onclick=\"jQuery('#").append(id).append("').buptreegrid('expandAll');return false;\" data-settings=\"iconClassname:'icon_small_expandall', flat:true\">全部展开</a>&nbsp;");
			html.append("<a href='#' onclick=\"jQuery('#").append(id).append("').buptreegrid('collapseAll');return false;\" data-settings=\"iconClassname:'icon_small_collapseall', flat:true\">全部收起</a>&nbsp;");
		}
		html.append("<a href='#' onclick=\"jQuery('#").append(id).append("').buptreegrid('reload');return false;\" data-settings=\"iconClassname:'icon-refresh', flat:true\" title='刷新'>刷新</a>&nbsp;");
		initToolbar(html);
		html.append("</div>");
		return tid;
	}
}
